package open.panxiong.shared.util

import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.net.wifi.WifiManager
import android.os.Build
import android.provider.Settings
import android.util.Log
import java.net.NetworkInterface
import java.util.Collections


/**
 * 获取本机全部的 IPv4 地址
 */
fun getLocalAllIpV4Address(): List<String> {
    val addressList = mutableListOf<String>()
    try {
        val networkInterface = Collections.list(NetworkInterface.getNetworkInterfaces())
        for (network in networkInterface) {
            val inetAddress = Collections.list(network.inetAddresses)
            for (address in inetAddress) {
                if (!address.isLoopbackAddress) {
                    val sAdder = address.hostAddress ?: continue
                    if (sAdder.indexOf(':') < 0) {
                        addressList.add(sAdder)
                    }
                }
            }
        }
    } catch (e: Exception) {
        Log.e("getLocalAllIpV4Address", "获取IP地址出错", e)
    }
    return addressList
}

/**
 * 打开热点界面
 */
fun openHotspotSettings(context: Context) {
    val intent = Intent()
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
    val comp = ComponentName(
        "com.android.settings",
        "com.android.settings.Settings\$TetherSettingsActivity"
    )
    intent.setComponent(comp)
    context.startActivity(intent)

}

/**
 * 打开wifi界面
 */
fun openWifiSettings(context: Context) {
    val intent: Intent
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        // 对于Android 10及以上版本
        intent = Intent(Settings.Panel.ACTION_INTERNET_CONNECTIVITY)
        context.startActivity(intent)
    } else {
        // 对于Android 10以下版本
        val packageName = "com.android.settings"
        val className = "com.android.settings.TetherSettings"
        intent = Intent(Intent.ACTION_MAIN)
        intent.addCategory(Intent.CATEGORY_LAUNCHER)
        intent.setClassName(packageName, className)
        val extraList: MutableList<String> = ArrayList()
        extraList.add("wifi_tether_settings")
        intent.putStringArrayListExtra(
            ":settings:fragment_args_key",
            extraList as ArrayList<String>
        )
        context.startActivity(intent)
    }
}

//fun getIpv4InConnectivityManager(context: Context) {
//    val ipAddressesList = mutableListOf<InetAddress>()
//    val connectivityManager =
//        context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
//    val network = connectivityManager.activeNetwork
//    val linkProperties = connectivityManager.getLinkProperties(network)
//    if (linkProperties != null) {
//        val ipAddresses = linkProperties.linkAddresses
//        for (ipAddress in ipAddresses) {
//            ipAddressesList.add(ipAddress.address)
//        }
//    }
//    Log.e("网络", ipAddressesList.toString())
//}
//
//fun getIpv4INNetworkCallback(context: Context) {
//    val ipAddressesList = mutableListOf<LinkAddress>()
//    val networkRequest = NetworkRequest.Builder().build()
//    (context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager?)?.registerNetworkCallback(
//        networkRequest,
//        object : ConnectivityManager.NetworkCallback() {
//            override fun onLinkPropertiesChanged(network: Network, linkProperties: LinkProperties) {
//                super.onLinkPropertiesChanged(network, linkProperties)
//                ipAddressesList.addAll(linkProperties.linkAddresses)
//            }
//        }
//    )
//    Log.e("网络", ipAddressesList.toString())
//}

fun getHotspotIpAddress(context: Context): String {
    val wifiManager = context.getSystemService(Context.WIFI_SERVICE) as WifiManager
    val dhcpInfo = wifiManager.dhcpInfo
    val ipAddressInt = dhcpInfo.serverAddress
    val ipAddress = (ipAddressInt and 0xff).toString() +
            "." + ((ipAddressInt shr 8) and 0xff) +
            "." + ((ipAddressInt shr 16) and 0xff) +
            "." + ((ipAddressInt shr 24) and 0xff)
    return ipAddress
}